from pylons import url, request
from pylons.i18n import lazy_ugettext as _
from chowa.lib.site.navigation import Tab, Menu, MenuItem
from chowa.model.organization import District
from hrms.model.person import Employee
from hrms.lib.base import Session

__vertical_tabs__ = [
    [
        Tab(caption=_('General'),
            ignore_params=['relation'],
            params=dict(controller='profile',
                        default__action='index')),
        Tab(caption=_('Employment history'),
            params=dict(controller='career/history',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Appointments'),
            params=dict(controller='career/appointments',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Dismissals'),
            params=dict(controller='career/dismissals',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Rankings'),
            params=dict(controller='career/rankings',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Awards'),
            params=dict(controller='career/awards',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Vacations'),
            params=dict(controller='holidays/vacations',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Holidays'),
            params=dict(controller='holidays/compensatory',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Education'),
            params=dict(controller='education/education',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Trainings'),
            params=dict(controller='education/trainings',
                        default__action='index',
                        relation='employee')),
        Tab(caption=_('Children'),
            params=dict(controller='children',
                        default__action='index',
                        relation='employee')),],
]

__primary_tabs__ = [
    [
        Tab(caption=_('General'),
            params=dict(controller='organization',
                        action='structure')),
        Tab(caption=_('Districts'),
            params=dict(controller='organization',
                        action='districts')),
        Tab(caption=_('Subordination'),
            params=dict(controller='org/subordination',
                        default__action='index')),],
    [
        Tab(caption=_('General'),
            ignore_params=['relation'],
            params=dict(controller='organization',
                        action='division')),
        Tab(caption=_('Employees'),
            params=dict(controller='org/employees',
                        default__action='index',
                        relation='division')),
        Tab(caption=_('Appointments'),
            params=dict(controller='career/appointments',
                        default__action='index',
                        relation='division')),
        Tab(caption=_('Dismissals'),
            params=dict(controller='career/dismissals',
                        default__action='index',
                        relation='division')),],
    [
        Tab(ignore_params=['relation'],
            caption=_('General'),
            params=dict(controller='organization',
                        action='division')),
        Tab(caption=_('Employees'),
            params=dict(controller='org/employees',
                        default__action='index',
                        relation='division')),
        Tab(caption=_('Appointments'),
            params=dict(controller='career/appointments',
                        default__action='index',
                        relation='division')),],
    [
        Tab(caption=_('Employees'),
            params=dict(controller='org/district',
                        action=['employees', 'former_employees'])),
        Tab(caption=_('Divisions'),
            params=dict(controller='org/divisions',
                        default__action='index',
                        relation='district')),
        Tab(params=dict(controller='org/district', action='offices'),
            caption=_('Offices'),
            description=_('Offices')
        ),
        Tab(caption=_('Errors'),
            params=dict(controller='dberrors', action='index')),],
    [
        Tab(caption=_('List'),
            params=dict(controller='orders/list',
                        action=['index', 'add'])),
        Tab(caption=_('Types'),
            params=dict(controller='orders/types',
                        default__action='index')),
        Tab(caption=_('Errors'),
            ignore_params=['id'],
            params=dict(controller='orders/list',
                        action='errors')),],
    [
        Tab(caption=_('References'),
            params=dict(controller='references',
                        default__action='index')),
        Tab(caption=_('Positions'),
            params=dict(controller='career/positions',
                        default__action='index')),
        Tab(caption=_('Award types'),
            params=dict(controller='career/awardtypes',
                        default__action='index')),
        Tab(caption=_('Vacation types'),
            params=dict(controller='holidays/vacationtypes',
                        default__action='index'))],
]

# dymanic load employee's district_id
# __menus__ must be function or lambda
def _district_id():
    try:
        identity = request.environ.get('repoze.who.identity')
        user = identity.get('user')
        employee = Session.query(Employee).get(user.person_id)
        return str(employee.division.district_id)
    except:
        # exception for anonymous
        # TODO: redirect to login form
        return ''

__menus__ = lambda: [
    Menu('hrms', caption=_('HRMS'),
         items = [
            MenuItem(caption=_('Structure'),
                     ignore_all=True,
                     params=dict(controller='organization',
                                 action='structure')),
            MenuItem(caption=_('Employees'),
                     ignore_all=True,
                     params=dict(controller='org/district',
                                 action='employees',
                                 id=_district_id())),
            MenuItem(caption=_('Reports'),
                     ignore_all=True,
                     params=dict(controller='brief',
                                 action='index')),
            MenuItem(caption=_('Orders'),
                     ignore_all=True,
                     params=dict(controller='orders/list',
                                 action='index',
                                 id='all')),
            MenuItem(caption=_('Education'),
                     ignore_all=True,
                     params=dict(controller='education/schools',
                                 action='index')),
            MenuItem(caption=_('References'),
                     ignore_all=True,
                     params=dict(controller='references',
                                 action='index')),]),
    Menu('worktime', caption=_('Working time'),
         items = [
            MenuItem(caption=_('Offices'),
                     ignore_all=True,
                     params=dict(controller='worktime/offices',
                                 action='index')),
            MenuItem(caption=_('Codes'),
                     ignore_all=True,
                     params=dict(controller='worktime/codes',
                                 action='index')),]),
]
